package com.cwits.wifi.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cwits.wifi.R;


/**
 * Created by Administrator on 2016/1/19.
 */
public class ProgressWindow extends PopupWindow {


    private ProgressBar mProgress;
    private TextView mTvTitle;
    private TextView mTvOk;
    private RelativeLayout mRlCancle, mRlOk;
    private View conentView;
    private Handler mHandler;
    private int mProgessint = 0;
    private boolean mIsStart = false;


    public ProgressWindow(final Activity context,View.OnClickListener playListener) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.zhuanma_dialog, null);

        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        mHandler = new Handler();
        mIsStart = false;
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果

        // 设置按钮的点击事件
        mRlCancle = (RelativeLayout) conentView.findViewById(R.id.zm_rl_canale);
        mRlOk = (RelativeLayout) conentView.findViewById(R.id.zm_rl_ok);
        mTvOk = (TextView) conentView.findViewById(R.id.zm_tv_ok);
        mProgress = (ProgressBar) conentView.findViewById(R.id.zm_progressbar);
        mTvTitle = (TextView) conentView.findViewById(R.id.zm_tv_title);
        mTvTitle.setText(context.getString(R.string.dialog_jiemaing));
        mRlOk.setOnClickListener(playListener);
        mRlCancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setProgress(float progress) {
        if (null == mProgress)
            return;
        int a = (int) progress;
        mProgessint = a;
        mProgress.setProgress(a);
    }

    public int getProgress(){
        return mProgress.getProgress();
    }

    public void startProgress(float progress) {
        if (null == mProgress||mIsStart)
            return;
        mIsStart = true;
        int a = (int) progress;
        mProgress.setProgress(a);
        mProgessint = a;
        mHandler.postDelayed(ProgressRunnable,1000);
    }

    private Runnable ProgressRunnable = new Runnable(){

        @Override
        public void run() {
            if(mProgessint >= 100){
                mProgessint = 100;
                mProgress.setProgress(mProgessint);
            }else{
                mProgessint = mProgessint + 1;
                mProgress.setProgress(mProgessint);
                mHandler.postDelayed(ProgressRunnable,1000);
            }
        }
    };

    public void setTvTitle(String str) {
        if (this.isShowing())
            mTvTitle.setText(str);
    }

    public void setPlayColor(int color) {
        if (this.isShowing())
            mTvOk.setTextColor(color);
    }


    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            // this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
            this.showAtLocation(parent, Gravity.CENTER, 0, 0);
        } else {
            this.dismiss();
        }
    }


}
